package GUI;

import Controllers.Build;
import Controllers.Exit;
import Controllers.RunSimulation;
import Controllers.StopSim;
import elevatorsimulator.WorldBuilder;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Controls the Administration Console and values passed from it
 */
public class AdminConsoleDisplay extends JPanel {
    //Label Strings
    String NAMECONLBL = "Naming Convention:";
    String UKAUSRADIOBTTN = "UK/Aus (G, 1, 2...)";
    String USARADIOBTTN = "USA (1,2,3...)";
    String NUMELEVATORLBL = "Number of elevators:";
    String NUMFLOORSLBL = "Number of Floors:";
    String ADMINBORDERTITLE = "Administration Console";
    String BUILDBTTNTITLE = "Build";
    String UNLOCKSIMBTTNTITLE = "Unlock";
    String LOCKSIMBTTNTITLE = "Lock";
    String EXITBTTNTITLE = "Exit";

    //Labels
    private JLabel numElevators;
    private JLabel numFloors;
    private JLabel nameCon;

    //Radio Buttons
    private JRadioButton ukAusRadioBttn;
    private JRadioButton usaRadioBttn;

    //Sliders
    private JSlider numElevatorSlider;
    private JSlider numFloorsSlider;

    //Buttons
    private JButton applyBttn;
    private JButton runSimBttn;
    private JButton stopSimBttn;
    private JButton exitBttn;

    //Button Group
    private ButtonGroup namingConBttnGroup;

    //Objects passed in
    WorldBuilder wb;
    ElevatorUI eds;
    FloorUI fd;

    /**
     * AdminUI Constructor
     *
     * @param wb  passign in World Builder
     * @param eds passing in ElevatorUI
     * @param fd  passing in FloorUI
     */
    public AdminConsoleDisplay(WorldBuilder wb, ElevatorUI eds, FloorUI fd) {
        this.wb = wb;
        this.eds = eds;
        this.fd = fd;
    }

    /**
     * Creates the Administration Display
     */
    public void viewCreator() {
        //**********************************************************************************************************
        //Naming Convention
        //**********************************************************************************************************
        nameCon = new JLabel(NAMECONLBL);
        //aus/uk radio button
        ukAusRadioBttn = new JRadioButton(UKAUSRADIOBTTN, true);

        //usa radio button
        usaRadioBttn = new JRadioButton(USARADIOBTTN, false);

        //Naming convention button group
        namingConBttnGroup = new ButtonGroup();
        namingConBttnGroup.add(ukAusRadioBttn);
        namingConBttnGroup.add(usaRadioBttn);

        //**********************************************************************************************************
        //Number of Elevators
        //**********************************************************************************************************
        numElevators = new JLabel(NUMELEVATORLBL);
        //Slider
        numElevatorSlider = new JSlider(JSlider.HORIZONTAL, 1, 5, 1);
        numElevatorSlider.setMajorTickSpacing(1);
        numElevatorSlider.setPaintTicks(true);
        numElevatorSlider.setSnapToTicks(true);
        numElevatorSlider.setPaintLabels(true);
        //numElevatorSlider.getValue();

        //**********************************************************************************************************
        //Number of Floors
        //**********************************************************************************************************
        numFloors = new JLabel(NUMFLOORSLBL);
        //Slider
        numFloorsSlider = new JSlider(JSlider.HORIZONTAL, 1, 20, 1);
        numFloorsSlider.setMajorTickSpacing(1);
        numFloorsSlider.setPaintTicks(true);
        numFloorsSlider.setSnapToTicks(true);
        numFloorsSlider.setPaintLabels(true);
        //numElevatorSlider.getValue();

        //**********************************************************************************************************
        //Panel Creation
        //**********************************************************************************************************
        //Naming Convention Panel
        JPanel nameConPanel = new JPanel();
        nameConPanel.setLayout(new GridLayout(3, 0));
        nameConPanel.add(nameCon);
        nameConPanel.add(ukAusRadioBttn);
        nameConPanel.add(usaRadioBttn);

        //Number of Elevators Panel
        JPanel numElevatorsPanel = new JPanel();
        numElevatorsPanel.setLayout(new GridLayout(2, 0));
        numElevatorsPanel.add(numElevators);
        numElevatorsPanel.add(numElevatorSlider);

        //Number of Floors Panel
        JPanel numFloorsPanel = new JPanel();
        numFloorsPanel.setLayout(new GridLayout(2, 0));
        numFloorsPanel.add(numFloors);
        numFloorsPanel.add(numFloorsSlider);

        //Master panel creation
        setLayout(new GridLayout(0, 1));
        setPreferredSize(new Dimension(400, 700));
        add(nameConPanel);
        add(numElevatorsPanel);
        add(numFloorsPanel);

        //**********************************************************************************************************
        //Apply, Run Simulation, Stop Simulation buttons and panel creation
        //**********************************************************************************************************
        //Apply Button
        applyBttn = new JButton();
        applyBttn.setText(BUILDBTTNTITLE);
        applyBttn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                updateUserOptions();
            }
        });

        //Run Simulation Button
        runSimBttn = new JButton();
        runSimBttn.setText(UNLOCKSIMBTTNTITLE);
        runSimBttn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new RunSimulation().run();
            }
        });

        //Exit Button
        exitBttn = new JButton();
        exitBttn.setText(EXITBTTNTITLE);
        exitBttn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new Exit().run();
            }
        });

        //Stop Simulation Button
        stopSimBttn = new JButton();
        stopSimBttn.setText(LOCKSIMBTTNTITLE);
        stopSimBttn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new StopSim().run();
            }
        });

        //Panel Creation
        JPanel simControlPanel = new JPanel();
        simControlPanel.setLayout(new GridLayout(0, 4));

        simControlPanel.add(applyBttn);
        simControlPanel.add(runSimBttn);
        simControlPanel.add(stopSimBttn);
        simControlPanel.add(exitBttn);

        setPreferredSize(new Dimension(450, 750));

        //add to master panel
        add(simControlPanel);
        //Border
        setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), ADMINBORDERTITLE));
    }

    /**
     * Number of floors slider return current value
     */
    public void numFloorSliderGetValue() {
        int value = 2;
        value = numFloorsSlider.getValue();
        wb.b.setNumFloors(value);

    }

    /**
     * Number of elevators slider return current value
     */
    public void numElevatorsSliderGetValue() {
        int value = 2;
        value = numElevatorSlider.getValue();
        wb.b.setNumElevators(value);

    }

    /**
     * Gets and sets the region supplied from radio buttons
     */
    public void namingConCheck() {
        if (ukAusRadioBttn.isSelected()) {
            wb.b.setBuildingZone('u');
        }
        if (usaRadioBttn.isSelected()) {
            wb.b.setBuildingZone('a');
        }
    }

    /**
     * Updates user choices and send values top objects
     */
    public void updateUserOptions() {
        numFloorSliderGetValue();
        numElevatorsSliderGetValue();
        namingConCheck();
        new Build().run(eds, fd);


    }
}

